<template>
    <section>
        <el-col :span="24">
            <el-form inline>
                <el-form-item>
                    <el-input v-model.trim="form.processing_code" size="small" style="width:200px;" placeholder="加工单号,多个用逗号隔开" @keyup.enter.native="searchBtn" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model.trim="form.sku_code" size="small" style="width:150px;" placeholder="sku" @keyup.enter.native="searchBtn" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.s_status" placeholder="审核状态" style="width: 100px;" size="small" clearable>
                        <el-option label="未审核" :value=0></el-option>
                        <el-option label="已审核" :value=1></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.f_status" placeholder="完结状态" size="small" style="width: 100px" clearable>
                        <el-option label="进行中" :value="0"></el-option>
                        <el-option label="已完结" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-popover placement="bottom" width="450" trigger="click">
                        <el-form label-width="110px" label-position="left">
                            <el-form-item label="款名">
                                <el-input v-model.trim="form.type_name" size="small" style="width:100%;" placeholder="输入款名称" clearable></el-input>
                            </el-form-item>
                            <el-form-item label="工厂名称">
                                <el-select v-model="form.supplier_id" filterable remote placeholder="输入搜索的工厂名称" size="small" style="width: 100%"
                                           :remote-method="searchFactoryBtn" :loading="searchLoading" clearable :disabled="user.source_from == 'gc'">
                                    <el-option v-for="(item,index) in factoryList" :label="item.factory_name" :value="item.supplier_id" :key="index" ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="RFID生成状态">
                                <el-select v-model="form.is_create" placeholder="RFID状态" style="" size="small" style="width: 100%" clearable>
                                    <el-option label="未设置" :value="0"></el-option>
                                    <el-option label="已生成" :value="1"></el-option>
                                    <el-option label="不生成" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="推送状态">
                                <el-select v-model="form.is_oms" placeholder="完结状态" style="" size="small" style="width: 100%" clearable>
                                    <el-option label="未推送" :value="0"></el-option>
                                    <el-option label="已推送" :value="1"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="下单日期">
                                <el-date-picker value-format="yyyy-MM-dd" v-model="form.add_date" type="daterange" size="small" style="width: 100%"
                                                range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                            </el-form-item>
                            <el-form-item label="理单员">
                                <el-select v-model="form.order_processor" filterable remote placeholder="输入搜索" size="small" style="width: 100%;"
                                           :remote-method="searchUserBtn" clearable>
                                    <el-option v-for="(item, index) in userList" :label="item.name" :value="item.name" :key="index" ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="到期预警">
                                <el-input v-model.trim="form.contract_date_days" size="small" style="width:100%;" placeholder="输入到期预警天数" clearable></el-input>
                            </el-form-item>
                        </el-form>
                        <el-badge :is-dot="(!!form.supplier_id || form.type_name!=='' || form.is_create!=='' || form.is_oms!=='' || !!form.add_date ||
                            !!form.order_processor || !!form.contract_date_days)" slot="reference" class="more-btn">
                            <el-button size="small" type="warning" plain>更多</el-button>
                        </el-badge>
                    </el-popover>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="el-icon-search" @click="searchBtn" size="small" :loading="btnLoading">
                        查询
                    </el-button>
                    <el-button type="text" class="el-icon-download" @click="getExcelFileBtn" size="small" :loading="downloading">
                        导出
                    </el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="24">
            <el-table ref="multipleTable" :data="data" style="width: 100%" :max-height="tableHeight" size="mini" border v-loading="loading"
                      :cell-style="cellStyle" :header-cell-style="headerCellStyle" :summary-method="getSummaries" show-summary @selection-change="selectionChange"
                      row-key="id" highlight-current-row>
                <el-table-column type="selection" align="center" width="55" :selectable="selectable" fixed></el-table-column>
                <el-table-column label="RFID状态"  prop="groups" width="120" fixed>
                    <template slot-scope="{ row }">
                        <el-badge :value="(row.is_create==0?'未设置':(row.is_create==1?'已生成':'不生成'))" style="margin-top: 10px;margin-right: 40px;"
                                  :type="(row.is_create==0?'info':(row.is_create==1?'success':'warning'))">
                            <el-tag v-if="row.groups==1" size="small">水洗唛</el-tag>
                            <el-tag v-else-if="row.groups==2" size="small">吊牌</el-tag>
                            <el-tag v-else size="small" type="info">其他</el-tag>
                        </el-badge>
                    </template>
                </el-table-column>
                <el-table-column label="加工单号"  prop="processing_code" width="160" fixed>
                    <template slot-scope="{ row }">
                        <el-badge :value="(row.f_status==0?'进行':'完结')" style="margin-top: 10px;margin-right: 40px;"
                                  :type="(row.f_status==1?'success':'danger')">
                            <el-tag size="small" :type="(row.status==0?'danger':'primary')" class="copy-processing-code"
                                    @click="copyBtn('.copy-processing-code', row.processing_code)">{{row.status==0 ? "废: ":""}}{{ row.processing_code }}</el-tag>
                        </el-badge>
                    </template>
                </el-table-column>
                <el-table-column label="图片" width="50" fixed>
                    <template slot-scope="{row}">
                        <el-image v-if="row.img" :src="row.img" :preview-src-list="[row.img]" style="height: 30px;display: inline-block" fit="contain"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="款号/SKU" prop="type_no" width="155" fixed>
                    <template slot-scope="{ row }">
                        <div>
                            <el-button type="text" style="color: #909399" size="small" class="el-icon-document-copy copy-type-no"
                                       @click="copyBtn('.copy-type-no', row.type_no)"></el-button>
                            <a @click="linkTo(row.type_no, 'pages_designEd')" style="color: #409eff">
                                {{row.type_no}}
                            </a>
                            <div v-if="row.sku_code" style="font-weight: bold">
                                <span>{{ row.sku_code }}</span>
                            </div>
                        </div>
                        <img v-if="user.source_from=='fs'" :src="kulogo" style="width:20px;height:20px;position:absolute;top:5px;right:10px;cursor: pointer;"
                             @click="openBtn(row.type_no, row.type_name)" />
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="s_status" width="80" fixed>
                    <template slot-scope="{ row }">
                        <div :style="{color:row.s_status == 1 ? '#67C23A' : (row.s_status == 2? '#F56C6C' : (row.s_status == 3 ? '#E6A23C': '#c5c8ce')),}">
                            {{row.s_status == 1 ? "已审核": (row.s_status == 0 ? "未审核" : (row.s_status == 2 ? "已结案" : (row.s_status == 3 ? "已同步" : ""))) }}
                        </div>
                        <div :style="{color: row.is_oms == 1 ? '#67C23A' : '#c5c8ce',}">
                            {{ row.is_oms == 1 ? "已推送" : "未推送" }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="下单数" align="center" prop="num" width="80"></el-table-column>
                <el-table-column label="增裁数" align="center" prop="zc_num" width="80"></el-table-column>
                <el-table-column label="报次数" align="center" prop="bao_number" width="80"></el-table-column>
<!--                <el-table-column label="溢装比数" align="center" prop="yc_num" width="80"></el-table-column>-->
                <el-table-column label="入库数" align="center" prop="qty" width="80">
                    <template slot-scope="{ row }">
                        <el-button type="text" size="small" @click="linkTo(row.processing_code, 'soa_returns')">{{row.qty}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="未入库数" align="center" prop="not_in_num" width="80"></el-table-column>
                <el-table-column label="到期预警" align="center" prop="contract_date_days" width="100"></el-table-column>
                <el-table-column label="合同号/供应商" width="170" prop="factory_contract_no" show-overflow-tooltip>
                    <template slot-scope="{ row }">
                        <el-button type="text" style="color: #909399" size="small" class="el-icon-document-copy copy-contract-number"
                                   @click="copyBtn('.copy-contract-number', row.factory_contract_no)"></el-button>
                        <a @click="linkTo(row.factory_contract_no, 'soa_cy_contract')">{{ row.factory_contract_no }}</a>
                        <div v-if="row.factory_name">{{ row.factory_name }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="大货核价编码" align="center" prop="verify_price_number" width="110">
                    <template slot-scope="{row}" v-if="row.verify_price_number">
                        <el-button type="text" style="color: #909399" size="small" class="el-icon-document-copy copy-verify-price-number"
                                   @click="copyBtn('.copy-verify-price-number', row.verify_price_number)"></el-button>
                        <a @click="linkTo(row.verify_price_number, 'soa_pricing_verify')" style="color: #409eff">
                            {{row.verify_price_number}}
                        </a>
                    </template>
                </el-table-column>
                <el-table-column label="总价" align="center" prop="countprice"></el-table-column>
                <el-table-column label="绣花厂" align="center" prop="embroidery_factory" show-overflow-tooltip></el-table-column>
                <el-table-column label="绣花厂单价" align="center" prop="embroidery_price" width="90"></el-table-column>
                <el-table-column label="采购通知单" align="center" prop="jy_purchase_number" width="90" show-overflow-tooltip></el-table-column>
                <el-table-column label="合同交期" prop="contract_date" align="center" width="100"></el-table-column>
                <el-table-column label="跟单人" align="center" prop="merchandiser"></el-table-column>
                <el-table-column label="理单人" align="center" prop="order_processor"></el-table-column>
                <el-table-column label="创建人" align="center" prop="adduser"></el-table-column>
                <el-table-column label="创建时间" align="center" prop="createtime" width="90"></el-table-column>
                <el-table-column label="印花厂" align="center" prop="printing_factory"></el-table-column>
                <el-table-column label="烫金厂" align="center" prop="bronzing_factory"></el-table-column>
                <el-table-column label="印金厂" align="center" prop="gold_printing_factory"></el-table-column>
                <el-table-column label="SKU单价" align="center" prop="unit_price"></el-table-column>
                <el-table-column label="运营下单日期" align="center" prop="add_date" width="100"></el-table-column>
                <el-table-column label="操作" align="center" fixed="right" width="160" v-if="user.source_from=='fs'">
                    <template slot-scope="{ row }">
                        <el-button type="text" size="mini" @click="pushBtn(row.processing_code, 1)" v-if="row.is_oms==0"
                                   :disabled="!(row.s_status!=0&&row.status==1&&row.f_status==0&&nodeActions.includes('push'))">推送</el-button>
                        <el-button type="text" size="mini" @click="pushBtn(row.processing_code, 0)" v-else
                                   :style="{color: row.s_status!=0&&row.status==1&&nodeActions.includes('push')&&row.qty==0?'red':''}"
                                   :disabled="!(row.s_status!=0&&row.status==1&nodeActions.includes('push')&&row.qty==0)">取消</el-button>
                        <el-button type="text" size="mini" @click="openProcessBtn(row.processing_code, 0)" v-if="row.status==1"
                                   :style="{color: row.is_oms==0&&nodeActions.includes('cancel')?'red':''}"
                                   :disabled="!(row.is_oms==0&&nodeActions.includes('cancel'))">作废</el-button>
                        <el-button type="text" size="mini" @click="openProcessBtn(row.processing_code, 1)" v-else
                                   :disabled="!(row.is_oms==0&&nodeActions.includes('cancel'))">启用</el-button>
                        &nbsp;
                        <el-dropdown trigger="hover" style="font-size: 12px;" @command="actionBtn($event, row)">
                            <span style="cursor: pointer;color: #409EFF;">
                                更多操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </span>
                            <el-dropdown-menu slot="dropdown" v-loading="dropdownBtnLoading">
                                <el-dropdown-item command="CT" icon="el-icon-document"
                                                  :disabled="!(row.status==1&&row.s_status!=0&&nodeActions.includes('apply'))">采 退</el-dropdown-item>
                                <el-dropdown-item command="FX" icon="el-icon-position"
                                                  :disabled="!(row.status==1&&row.s_status!=0&&nodeActions.includes('apply'))">返 修</el-dropdown-item>
                                <el-dropdown-item command="BC" icon="el-icon-postcard"
                                                  :disabled="!(row.s_status!=0&&row.f_status==0&&row.status==1&&nodeActions.includes('apply'))">报 次</el-dropdown-item>
                                <!--el-dropdown-item command="PC" icon="el-icon-tickets" :disabled="!(row.s_status!=0&&row.f_status==0&&row.status==1)">批 次</el-dropdown-item-->
                                <el-dropdown-item command="ZC" icon="el-icon-scissors"
                                                  :disabled="!(row.s_status!=0&&row.source_type<5&&row.status==1&&nodeActions.includes('apply'))">增 裁</el-dropdown-item>
                                <el-dropdown-item command="END" icon="el-icon-video-pause"
                                                  :disabled="!(row.s_status!=0&&row.f_status==0&&row.status==1&&row.qty>0&&nodeActions.includes('lock'))">完 结</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <!--翻页栏-->
            <div style="margin-top: 4px;overflow: hidden;">
                <template v-if="user.source_from=='fs'">
                    <el-button type="primary" :disabled="!selectData.length" size="mini" :loading="btnLoading" @click="createRfid(1)">生成洗水唛</el-button>
                    <el-button type="primary" :disabled="!selectData.length" size="mini" :loading="btnLoading" @click="createRfid(2)">生成吊牌</el-button>
                </template>
                <Page style="float: right" :total="total" :current="page" :page-size="limit" show-sizer :page-size-opts="[20, 50, 100, 500]" show-total @on-change="pageChange"
                      @on-page-size-change="pageSizeChange"></Page>
            </div>
        </el-col>

        <!--巨益商品信息 库存-->
        <el-dialog title="商品库存" :visible.sync="juyiVisible" width="700px" :close-on-click-modal="false">
            <div>
                <div style="display: flex;justify-content: space-between;margin-bottom: 10px;">
                    <div>款号：{{ typeNo }}</div>
                    <div>款名：{{ typeName }}</div>
                </div>
                <el-table height="300" :data="juyiData" border size="mini" style="width: 100%">
                    <el-table-column prop="product_code" align="center" label="SKU"></el-table-column>
                    <el-table-column prop="num" align="center" label="库存"></el-table-column>
                    <el-table-column prop="use_num" align="center" label="可销数">
                        <template slot-scope="{ row }">
                            <span style="font-weight: 600" :style="{ color: row.use_num < 0 ? 'red' : '#67C23A' }">{{ row.use_num || 0 }}</span>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-dialog>
        <!--采退、返修-->
        <Modal v-model="returnVisible" :title="(commandType=='CT'?'采购退货':'返厂维修')" width="700px" :loading="true" :mask-closable="false"
            @on-cancel="returnCloseBtn">
            <div>
                <div style="display: flex;justify-content: space-between;margin-bottom: 10px;">
                    <div>加工单：<span style="font-weight: bold;font-size: 14px;">{{ processingCode }}</span></div>
                    <div>加工厂：<span style="font-weight: bold;font-size: 14px;">{{ factoryName }}</span></div>
                </div>
                <el-table :data="skuData" border size="mini" style="width: 100%" max-height="400px" @selection-change="selectSkuChange">
                    <el-table-column type="selection" width="55" align="center" :selectable="selectableSku"></el-table-column>
                    <el-table-column prop="type_no" align="center" label="款号" width="120"></el-table-column>
                    <el-table-column prop="type_name" align="center" label="款名" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="sku_code" align="center" label="SKU" width="150"></el-table-column>
                    <el-table-column prop="num" align="center" :label="commandType=='CT' ? '计划退货数' : '计划返厂数'" width="120">
                        <template slot-scope="{ row, $index }">
                            <el-input type="number" v-model="row.num" placeholder="0" size="mini" @input=""></el-input>
                        </template>
                    </el-table-column>
                </el-table>
                <el-input type="textarea" v-model.trim="remark" :rows="1" placeholder="备注信息（必填）" style="margin-top: 4px;"></el-input>
            </div>
            <div slot="footer">
                <i-button @click="returnCloseBtn">退 出</i-button>
                <i-button type="primary" :disabled="!selectSkuData.length" :loading="btnLoading" @click="returnSubmitBtn">提 交</i-button>
            </div>
        </Modal>
        <!--报次-->
        <Modal v-model="baociVisible" title="报次数量" width="700px" :loading="true" :mask-closable="false"
               @on-cancel="returnCloseBtn">
            <div>
                <div style="display: flex;justify-content: space-between;margin-bottom: 10px;">
                    <div>加工单：<span style="font-weight: bold;font-size: 14px;">{{ processingCode }}</span></div>
                    <div>加工厂：<span style="font-weight: bold;font-size: 14px;">{{ factoryName }}</span></div>
                </div>
                <el-table :data="skuData" border size="mini" style="width: 100%" max-height="400px" @selection-change="selectSkuChange">
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column prop="type_no" align="center" label="款号" width="120"></el-table-column>
                    <el-table-column prop="type_name" align="center" label="款名" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="sku_code" align="center" label="SKU" width="150"></el-table-column>
                    <el-table-column prop="num" align="center" label="报次数" width="120">
                        <template slot-scope="{ row, $index }">
                            <el-input type="number" v-model="row.num" placeholder="0" size="mini" @input=""></el-input>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div slot="footer">
                <i-button @click="returnCloseBtn">退 出</i-button>
                <i-button type="primary" :disabled="!selectSkuData.length" :loading="btnLoading" @click="baociSubmitBtn">提 交</i-button>
            </div>
        </Modal>
        <!--增裁-->
        <Modal v-model="zengcVisible" title="增裁补码" width="700px" :loading="true" :mask-closable="false"
               @on-cancel="returnCloseBtn">
            <div>
                <div style="display: flex;justify-content: space-between;margin-bottom: 10px;">
                    <div>加工单：<span style="font-weight: bold;font-size: 14px;">{{ processingCode }}</span></div>
                    <div>加工厂：<span style="font-weight: bold;font-size: 14px;">{{ factoryName }}</span></div>
                </div>
                <el-table :data="skuData" border size="mini" style="width: 100%" max-height="400px" @selection-change="selectSkuChange">
                    <el-table-column type="selection" width="55" align="center" :selectable="selectableSku"></el-table-column>
                    <el-table-column prop="type_no" align="center" label="款号" width="120"></el-table-column>
                    <el-table-column prop="type_name" align="center" label="款名" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="sku_code" align="center" label="SKU" width="150"></el-table-column>
                    <el-table-column prop="num" align="center" label="补码数量" width="120">
                        <template slot-scope="{ row, $index }">
                            <el-input type="number" v-model="row.num" placeholder="0" size="mini" @input=""></el-input>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div slot="footer">
                <i-button @click="returnCloseBtn">退 出</i-button>
                <i-button type="primary" :disabled="!selectSkuData.length" :loading="btnLoading" @click="zengcSubmitBtn">提 交</i-button>
            </div>
        </Modal>
        <!--批次-->
        <Modal v-model="batchVisible" title="设置批次" width="500px" :loading="true" :mask-closable="false">
            <el-form label-width="90px" @submit.native.prevent>
                <el-form-item label="选择批次">
                    <el-select v-model="batchId" placeholder="请选择" size="small" clearable>
                        <el-option v-for="item in batchData" :key="item.id" :label="item.batch_date" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <i-button @click="batchVisible=false;">退 出</i-button>
                <i-button type="primary" :loading="btnLoading" @click="setBatchBtn">提 交</i-button>
            </div>
        </Modal>

        <!--导出-->
        <el-dialog title="导出管理" :visible.sync="exportVisible" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="exportCloseBtn" width="800px">
            <el-table :data="excelFile" style="width: 100%" height="400px" size="small">
                <el-table-column type="index" width="50"></el-table-column>
                <el-table-column prop="create_user" label="创建人" width="90"></el-table-column>
                <el-table-column prop="create_time" label="导出时间" width="135"></el-table-column>
                <el-table-column prop="name" label="文件" width="220"></el-table-column>
                <el-table-column prop="status" label="操作">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status == 0 && exportCache.process < 100">
                            <el-progress :text-inside="true" :stroke-width="16" status="success" :percentage="exportCache.process"></el-progress>
                        </span>
                        <span v-else>
                            <el-button type="text" class="el-icon-download" size="small" @click="downloadBtn(scope.row.name)">&nbsp;下载</el-button>
                        </span>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer">
                <el-button @click="exportCloseBtn" size="small" plain>退 出</el-button>
                <el-button type="primary" @click="exportDataBtn" size="small" :loading="saveLoading"
                           :disabled="exportCache.file && exportCache.process<100">添加计划</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {
    processingList,
    processingRfidCreate,
    processingOpen,
    processingPush,
    processingEnd,
    processingBatchSet,
    processingSkuList,
    processingGoodReturn,
    processingBaociSet,
    processingZengcSet,
    processingExcelList, processingExportCache, processingExportData
} from "@/api/produce/processing";
import {batchList} from "@/api/produce/batch";
import {juyiList} from "@/api/oms/juyi";
import kulogo from "@/assets/images/kulogo.png";
import {factoryList} from "@/api/produce/factory";
import {copyText} from "../../libs/tools";
import {userList} from "../../api/user";

export default {
    name: "produce_processing",
    data() {
        return {
            kulogo,
            tableHeight: window.innerHeight - 210,
            selectIndex: 0,
            btnLoading: false,
            loading: false,
            page: 1,
            limit: 20,
            total: 0,
            data: [],
            user: {},
            nodeActions: [],

            form: {
                processing_code: "",
                sku_code: "",
                type_name: "",
                factory_name:"",
                supplier_id: "",
                s_status: "",
                is_create: "",
                f_status: "",
                is_oms: "",
                add_date: "",
            },
            userList: [],
            searchLoading: false,
            factoryList: [],
            selectData: [],

            typeNo: "",
            typeName: "",
            juyiData: [],
            juyiVisible: false,

            commandType: "",//下拉菜单类型
            dropdownBtnLoading: false,
            batchData: [],//批次列表
            batchId: "",
            processingCode: "",
            factoryName: "",
            batchVisible: false,

            selectSkuData: [],
            skuData: [],//采退、返修、报次
            remark: "",
            returnVisible: false,
            baociVisible: false,
            zengcVisible: false,

            //导出
            saveLoading: false,
            downloading: false,
            excelFile: [],
            exportCache: {},
            exportVisible: false,
            exportTimeout: null,
        }
    },
    created() {
        this.user = this.$store.getters.user;
        this.nodeActions = this.user.node_actions[this.$route.meta.id] ? this.user.node_actions[this.$route.meta.id] : [];
        if(this.user.source_from == 'gc'){//用户来源：工厂
            this.form.supplier_id = this.user.supplier_id;
        }
    },
    updated() {
        this.$nextTick(() => {
            this.$refs.multipleTable.doLayout();
        });
    },
    watch:{
        // 监听路由参数的变化
        '$route.params.processing_code': {
            immediate: true,
            handler(val) {
                if(!!val && val != this.form.processing_code){
                    this.$set(this.form, 'processing_code', val);
                    this.searchBtn();
                }
            }
        },
    },
    methods: {
        //查询用户
        searchUserBtn(val){
            if(val == ""){
                return;
            }
            this.searchLoading = true;
            userList({page: 1, limit:20,source_from: 'fs', name: val}).then((res) =>{
                this.searchLoading = false;
                this.userList = res.data.data;
            }).catch(() =>{
                this.searchLoading = false;
            })
        },
        //搜索加工厂
        searchFactoryBtn(val){
            if(val == ""){
                return;
            }
            this.getFactoryData(val);
        },
        getFactoryData(name){
            factoryList({page: 1, limit: 200, factory_name: name, status: 0}).then((res) =>{
                this.factoryList = res.data.data;
            })
        },
        //搜索按钮
        searchBtn() {
            this.page = 1;
            this.getData();
        },
        //翻页
        pageChange(page) {
            this.page = page;
            this.getData();
        },
        //页数
        pageSizeChange(pageSize){
            this.limit = pageSize;
        },
        //获取数据
        getData() {
            let param = {
                page: this.page,
                limit: this.limit
            };
            param = Object.assign(this.form, param);
            this.btnLoading = true;
            this.loading = true;
            processingList(param).then((res) => {
                this.btnLoading = false;
                this.loading = false;
                this.total = res.data.total;
                this.data = res.data.data;
            }).catch(() => {
                this.btnLoading = false;
                this.loading = false;
            });
        },
        //获取批次列表
        getBatchData(){
            batchList({page: 1, limit: 20, status: 1}).then((res) =>{
                this.batchData = res.data.data;
            }).catch(()=>{

            })
        },
        //获取加工单对应的sku列表
        getSkuData(processingCode, visible){
            this.loading = true;
            processingSkuList({processing_code: processingCode}).then((res) =>{
                this.loading = false;
                this.skuData = res.data;
                this[visible] = true;
            }).catch(()=>{
                this.loading = false;
            })
        },
        cellStyle(row) {
            if (row.column.label == "未入库数") {
                return {color: "red", background: "#F5F7FA"};
            }
        },
        headerCellStyle(row) {
            if (row.column.label == "未入库数") {
                return {color: "red", background: "#F0F9EB"};
            }else if (["下单数","增裁数","报次数","入库数","溢装比数"].includes(row.column.label)) {
                return {background: "#F0F9EB"};
            }else{
                return {background: "#F3F4F7"}
            }
        },
        copyBtn(classTag, content){
            copyText(classTag, content);
            this.$message.success("复制成功: "+content)
        },
        linkTo(val, page){
            switch (page){
                case "pages_designEd":
                    this.$router.push({
                        name: page,
                        params: {id: val, isEdit: 0}
                    })
                    break;
                case "soa_returns":
                    this.$router.push({
                        name: page,
                        params: {processing_code: val}
                    })
                    break;
                case "soa_cy_contract":
                    this.$router.push({
                        name: page,
                        params: {factory_contract_no: val}
                    })
                    break;
                case "soa_pricing_verify":
                    this.$router.push({
                        name: page,
                        params: {verify_price_number: val}
                    });
                    break;
            }
        },
        //合计
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计";
                    return;
                }
                const values = data.map((item) => Number(item[column.property]));
                if (!values.every((value) => isNaN(value)) && (["下单数","增裁数","报次数","入库数","溢装比数","未入库数"].includes(column.label))) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] = sums[index];
                } else {
                    sums[index] = "";
                }
            });
            return sums;
        },
        //查询巨益商品库存信息
        openBtn(typeNo, typeName){
            this.typeNo = typeNo;
            this.typeName = typeName;
            this.loading = true;
            this.juyiData = [];
            let obj = {
                page: 1,
                limit: 20,
                sku_code: typeNo
            };
            juyiList(obj).then((res) =>{
                this.loading = false;
                this.juyiVisible = true;
                if(res.code === 0){
                    this.juyiData = res.data.data;
                }
            }).catch(()=>{
                this.loading = false;
            })
        },
        //对复选框禁用
        selectable(row, rowIndex){
            if([1, 2].includes(row.is_create)){
                return false;
            }else{
                return true;
            }
        },
        selectableSku(row, rowIndex){
            if(row.num && row.num>0){
                return true;
            }else{
                return false;
            }
        },
        selectionChange(val) {
            this.selectData = val;
        },
        selectSkuChange(val){
            this.selectSkuData = val;
        },
        //生成rfid
        createRfid(status){
            let msg = status == 1 ? "生成RFID洗水唛" : (status == 22 ? "无需生成RFID" : status == 2 ? "生成RFID吊牌" : "");
            this.$confirm(`确定：${msg}?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.btnLoading = true;
                let ids = [];
                this.selectData.forEach((item) =>{
                    ids.push({
                        id: item.id,
                        processing_code: item.processing_code,
                        type_name: item.type_name,
                        type_no: item.type_no,
                        color_code: item.color_code,
                        size_code: item.size_code,
                        num: item.num,
                        sku_code: item.sku_code,
                        factory_name: item.factory_name,
                        supplier_id: item.supplier_id
                    });
                });
                processingRfidCreate({data: JSON.stringify(ids), status: status}).then((res) =>{
                    this.btnLoading = false;
                    if(res.code === 0){
                        this.$message.success("创建成功");
                        this.getData();
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(()=>{
                    this.btnLoading = false;
                })
            }).catch(() => {

            });
        },
        //启用、禁用
        openProcessBtn(processingCode, status){
            this.$confirm(`【${status == 0 ? "作废" : "启用"}】该加工单, 是否继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.loading = true;
                processingOpen({processing_code: processingCode, status: status}).then((res)=>{
                    this.loading = false;
                    if(res.code === 0){
                        this.$message.success(status == 0 ? "作废成功" : "启用成功");
                        this.getData();
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(()=>{
                    this.loading = false;
                })
            }).catch(() => {

            });
        },
        //推送、取消
        pushBtn(processingCode, status){
            this.$confirm(`【${status == 1 ? "推送" : "取消"}】OMS采购单计划, 是否继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.loading = true;
                processingPush({processing_code: processingCode, status: status}).then((res) =>{
                    this.loading = false;
                    if(res.code === 0){
                        this.$message.success(status == 0 ? "【取消计划】成功" : "【推送计划】成功");
                        this.getData();
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(()=>{
                    this.loading = false;
                })
            }).catch(() => {

            });
        },
        returnCloseBtn(){
            this.returnVisible = false;
            this.baociVisible = false;
            this.zengcVisible = false;
            this.selectSkuData = [];
            this.remark = "";
        },
        //更多操作
        actionBtn(command, row){
            this.processingCode = row.processing_code;
            this.factoryName = row.factory_name;
            this.commandType = command;
            switch (command){
                case "CT"://采退事件
                case "FX"://返修事件
                    this.getSkuData(row.processing_code, "returnVisible");
                    break;
                case "BC"://报次事件
                    this.getSkuData(row.processing_code, "baociVisible");
                    break;
                case "ZC"://增裁事件
                    this.getSkuData(row.processing_code, "zengcVisible");
                    break;
                    break;
                case "PC"://批次事件
                    this.batchVisible = true;
                    break;
                case "END"://强制完结事件
                    this.$confirm("确定：强制完结?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.dropdownBtnLoading = true;
                        processingEnd({processing_code: row.processing_code}).then((res) =>{
                            this.dropdownBtnLoading = false;
                            if(res.code === 0){
                                this.$message.success(res.msg);
                                this.getData();
                            }else{
                                this.$message.error(res.msg);
                            }
                        }).catch(() =>{
                            this.dropdownBtnLoading = false;
                        })
                    }).catch(() => {

                    });
                    break;
            }
        },
        //设置批次
        setBatchBtn(){
            let obj ={
                batch_id: this.batchId,
                processing_code: this.processingCode
            };
            this.btnLoading = true;
            processingBatchSet(obj).then((res) =>{
                this.btnLoading = false;
                if(res.code === 0){
                    this.$message.success("设置成功");
                    this.batchVisible = false;
                    this.getData();
                }else{
                    this.$message.error(res.msg);
                }
            }).catch(()=>{
                this.btnLoading = false;
            })
        },
        //提交采退、返修
        returnSubmitBtn(){
            let obj = {
                data: JSON.stringify(this.selectSkuData),
                type: this.commandType,
                remark: this.remark
            };
            let flag = false;
            this.selectSkuData.forEach((item)=>{
                if(Number(item.num) <= 0){
                    flag = true;
                }
            });
            if(flag){
                this.$message.warning("计划数量必须【大于0】");return;
            }
            if(!this.remark){
                this.$message.warning("请输入备注信息");return;
            }
            this.$confirm(`确认计划：【${this.commandType == "FX" ? "返修" : "采退"}】, 是否继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.btnLoading = true;
                processingGoodReturn(obj).then((res) =>{
                    this.btnLoading = false;
                    if(res.code === 0){
                        this.$message.success("创建成功");
                        this.returnVisible = false;
                        this.searchBtn();
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(()=>{
                    this.btnLoading = false;
                })
            }).catch(() => {

            });
        },
        //提交报次数量
        baociSubmitBtn(){
            let obj = {
                data: JSON.stringify(this.selectSkuData),
            };
            let flag = false;
            this.selectSkuData.forEach((item)=>{
                if(Number(item.num) <= 0){
                    flag = true;
                }
            });
            if(flag){
                this.$message.warning("报次数量必须【大于0】");return;
            }
            this.btnLoading = true;
            processingBaociSet(obj).then((res) =>{
                this.btnLoading = false;
                if(res.code === 0){
                    this.$message.success("报次成功");
                    this.baociVisible = false;
                    this.getData();
                }else{
                    this.$message.error(res.msg);
                }
            }).catch(()=>{
                this.btnLoading = false;
            })
        },
        //增裁数量
        zengcSubmitBtn(){
            let obj = {
                data: JSON.stringify(this.selectSkuData),
            };
            let flag = false;
            this.selectSkuData.forEach((item)=>{
                if(Number(item.num) <= 0){
                    flag = true;
                }
            });
            if(flag){
                this.$message.warning("增裁数量必须【大于0】");return;
            }
            this.$confirm(`确认计划：【增裁】, 是否继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.btnLoading = true;
                processingZengcSet(obj).then((res) =>{
                    this.btnLoading = false;
                    if(res.code === 0){
                        this.$message.success("增裁计划成功, 请耐心等待增裁审批通过...");
                        this.zengcVisible = false;
                        this.getData();
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(()=>{
                    this.btnLoading = false;
                })
            }).catch(() => {

            });
        },

        //导出
        getExcelFileBtn(){
            this.downloading = true;
            processingExcelList().then((res) =>{
                this.downloading = false;
                this.excelFile = res.data.data;
                this.exportCache = res.data.cache;
                this.exportVisible = true;
                if(this.exportCache.file && this.exportCache.process<100){
                    this.loopExportCache();
                }
            }).catch(()=>{
                this.downloading = false;
            })
        },
        exportCloseBtn(){
            clearTimeout(this.exportTimeout);
            this.exportTimeout = null;
            this.exportVisible = false;
        },
        loopExportCache(){
            let that = this;
            processingExportCache().then((res) =>{
                if(res.data.file && res.data.process<100){
                    this.exportCache = res.data;
                    this.exportTimeout = setTimeout(function (){
                        that.loopExportCache();
                    },2000)
                }else{
                    this.exportCache = {};
                }
            })
        },
        exportDataBtn(){
            this.$confirm(`根据当前查询条件,共${this.total}条数据，添加导出计划, 确定继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                processingExportData(this.form).then((res) =>{
                    if(res.code === 0){
                        this.$message.success("计划已添加");
                        this.getExcelFileBtn();
                    }else{
                        this.$message.error(res.msg);
                    }
                })
            }).catch(() => {});
        },
        downloadBtn(name){
            window.open('/api/tool/fileExport'+`?path=cache/processing&type=file&name=${name}`, '_self');
        },
    },
    mounted() {
        this.getFactoryData("");
        this.searchBtn();
        this.getBatchData();
    }
}
</script>

<style scoped>
/* 解决有合计行时，横向滚动条在左侧、右侧固定列中无法选中问题*/
/deep/ .el-table__fixed, /deep/ .el-table__fixed-right{
    pointer-events: none;
}
/deep/ .el-table__fixed >*, /deep/ .el-table__fixed-right >*{
    pointer-events: all;
}

.more-btn /deep/ .el-badge__content.is-fixed.is-dot{
    top: 5px;
}
.el-image /deep/ .el-icon-circle-close:before{
    color: white;
}
/*弹窗图片，遮罩层 层级bug*/
/deep/ .el-table__fixed-body-wrapper{
    z-index: unset;
}
</style>
